package com.zx.mobile.service.proxy;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.cookie.DateUtils;

import com.jd.jr.pay.gate.signature.util.BASE64;
import com.jd.jr.pay.gate.signature.util.SignUtil;
import com.jd.jr.pay.gate.signature.util.ThreeDesUtil;
import com.zx.jd.model.BasePayOrderInfo;
import com.zx.jd.util.CertUtil;
import com.zx.jd.util.PropertyUtils;
import com.zx.jd.util.StringEscape;
import com.zx.map.service.model.MerchantInfoDetailModel;
import com.zx.mobile.domain.NocardConfirmPayDomain;
import com.zx.platform.utils.PmpUtils;

/**
 * 京东无卡支付
 * @author wen
 *
 */
public class JdNocardPayProxy {

    /**
     * 接口编码转系统银行编码 102 中国工商银行 103 中国农业银行 104 中国银行 105 中国建设银行 301 交通银行 302 中信银行
     * 303 中国光大银行 305 中国民生银行 306 广东发展银行 307 深圳发展银行 308 招商银行 309 兴业银行 310
     * 上海浦东发展银行 403 中国邮政储蓄银行
     *
     * @param bankcode
     * @return
     */
    public static String transformBankCode(String bankcode) {
        String bankCode = null;
        if ("ICBC".equals(bankcode)) {
            bankCode = "102";
        } else if ("ABC".equals(bankcode)) {
            bankCode = "103";
        } else if ("BOC".equals(bankcode)) {
            bankCode = "104";
        } else if ("CCB".equals(bankcode)) {
            bankCode = "105";
        } else if ("".equals(bankcode)) {//交通银行不支持
            bankCode = "301";
        } else if ("ECITIC".equals(bankcode)) {
            bankCode = "302";
        } else if ("CEB".equals(bankcode)) {
            bankCode = "303";
        } else if ("CMBC".equals(bankcode)) {
            bankCode = "305";
        } else if ("GDB".equals(bankcode)) {//modify 20151008 广东发展银行
            bankCode = "306";
        } else if ("".equals(bankcode)) {//深圳发展银行不支持
            bankCode = "307";
        } else if ("CMBCHINA".equals(bankcode)) {
            bankCode = "308";
        } else if ("CIB".equals(bankcode)) {
            bankCode = "309";
        } else if ("SPDB".equals(bankcode)) {
            bankCode = "310";
        } else if ("POST".equals(bankcode)) {
            bankCode = "403";
        } else if ("PINGAN".equals(bankcode)) {
            bankCode = "404";
        } else if ("BCCB".equals(bankcode)) {
            bankCode = "405";
        } else if ("SHB".equals(bankcode)) {
            bankCode = "406";
        } else if ("HXB".equals(bankcode)) {
            bankCode = "407";
        }
        return bankCode;
    }



    /**
     * 获取无卡支付web链接
     * @param payDomain
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getNoCardPayUrl(NocardConfirmPayDomain payDomain,HttpServletRequest request,MerchantInfoDetailModel merchantInfo) throws UnsupportedEncodingException {
    	
    	BasePayOrderInfo basePayOrderInfo = new BasePayOrderInfo();
		basePayOrderInfo.setVersion(PropertyUtils.getProperty("version"));
		basePayOrderInfo.setMerchant(PropertyUtils.getProperty("wepay.merchant.num"));
		basePayOrderInfo.setDevice(PropertyUtils.getProperty("device"));
		
		basePayOrderInfo.setTradeNum(payDomain.getOrderId());
		
		basePayOrderInfo.setTradeName(PropertyUtils.getProperty("tradeName"));
		basePayOrderInfo.setTradeDesc(PropertyUtils.getProperty("tradeDesc"));
		basePayOrderInfo.setTradeTime(DateUtils.formatDate(new Date(), "yyyyMMddHHmmss"));
		int amount = new BigDecimal(payDomain.getAmount()).multiply(
                new BigDecimal("100")).intValue();
		basePayOrderInfo.setAmount(String.valueOf(amount));
		basePayOrderInfo.setCurrency(PropertyUtils.getProperty("currency"));
		basePayOrderInfo.setNote(PropertyUtils.getProperty("note"));
		basePayOrderInfo.setNotifyUrl(PropertyUtils.getProperty("callBackUrl"));
		basePayOrderInfo.setCallbackUrl(PropertyUtils.getProperty("frontCallBackUrl"));
		basePayOrderInfo.setIp(PmpUtils.getHostIp(null));
		basePayOrderInfo.setUserType(PropertyUtils.getProperty("userType"));
		basePayOrderInfo.setUserId(merchantInfo.getMerchantId());
		//取默认7天
		//basePayOrderInfo.setExpireTime(PropertyUtils.getProperty("expireTime"));
		basePayOrderInfo.setOrderType(PropertyUtils.getProperty("orderType"));
		basePayOrderInfo.setIndustryCategoryCode(PropertyUtils.getProperty("industryCategoryCode"));
		basePayOrderInfo.setSpecCardNo(payDomain.getCardNo());
		basePayOrderInfo.setSpecId(merchantInfo.getLegalIdentityno());
		basePayOrderInfo.setSpecName(merchantInfo.getMerchantName());

		basePayOrderInfo.setVendorId(PropertyUtils.getProperty("vendorId"));
		basePayOrderInfo.setGoodsInfo("");
		basePayOrderInfo.setOrderGoodsNum("");
		basePayOrderInfo.setTermInfo("");
		basePayOrderInfo.setReceiverInfo("");
		JdNocardPayProxy.filterCharProcess(basePayOrderInfo);

		String oriUrl = PropertyUtils.getProperty("wepay.merchant.saveUrl");
		String priKey = PropertyUtils.getProperty("wepay.merchant.rsaPrivateKey");
		String desKey = PropertyUtils.getProperty("wepay.merchant.desKey");
		String cert = CertUtil.getCert();
		// 有证书则证书验证模式、无则配置模式
		if (cert != null && !cert.equals("")) {
			basePayOrderInfo.setCert(cert);
		}

		List<String> unSignedKeyList = new ArrayList<String>();
		unSignedKeyList.add("sign");

		basePayOrderInfo.setSign(SignUtil.signRemoveSelectedKeys(basePayOrderInfo, priKey, unSignedKeyList));

		byte[] key = BASE64.decode(desKey);

		if (StringUtils.isNotBlank(basePayOrderInfo.getDevice())) {
			basePayOrderInfo.setDevice(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getDevice()));
		}
		basePayOrderInfo.setTradeNum(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getTradeNum()));
		if (StringUtils.isNotBlank(basePayOrderInfo.getTradeName())) {
			basePayOrderInfo.setTradeName(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getTradeName()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getTradeDesc())) {
			basePayOrderInfo.setTradeDesc(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getTradeDesc()));
		}
		basePayOrderInfo.setTradeTime(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getTradeTime()));
		basePayOrderInfo.setAmount(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getAmount()));
		basePayOrderInfo.setCurrency(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getCurrency()));
		if (StringUtils.isNotBlank(basePayOrderInfo.getNote())) {
			basePayOrderInfo.setNote(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getNote()));
		}
		basePayOrderInfo.setCallbackUrl(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getCallbackUrl()));
		basePayOrderInfo.setNotifyUrl(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getNotifyUrl()));
		basePayOrderInfo.setIp(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getIp()));
		if (StringUtils.isNotBlank(basePayOrderInfo.getUserType())) {
			basePayOrderInfo.setUserType(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getUserType()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getUserId())) {
			basePayOrderInfo.setUserId(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getUserId()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getExpireTime())) {
			basePayOrderInfo.setExpireTime(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getExpireTime()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getOrderType())) {
			basePayOrderInfo.setOrderType(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getOrderType()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getIndustryCategoryCode())) {
			basePayOrderInfo
					.setIndustryCategoryCode(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getIndustryCategoryCode()));
		}

		if (StringUtils.isNotBlank(basePayOrderInfo.getSpecCardNo())) {
			basePayOrderInfo.setSpecCardNo(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getSpecCardNo()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getSpecId())) {
			basePayOrderInfo.setSpecId(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getSpecId()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getSpecName())) {
			basePayOrderInfo.setSpecName(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getSpecName()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getVendorId())) {
			basePayOrderInfo.setVendorId(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getVendorId()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getGoodsInfo())) {
			basePayOrderInfo.setGoodsInfo(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getGoodsInfo()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getOrderGoodsNum())) {
			basePayOrderInfo.setOrderGoodsNum(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getOrderGoodsNum()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getTermInfo())) {
			basePayOrderInfo.setTermInfo(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getTermInfo()));
		}
		if (StringUtils.isNotBlank(basePayOrderInfo.getReceiverInfo())) {
			basePayOrderInfo.setReceiverInfo(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getReceiverInfo()));
		}

		if (StringUtils.isNotBlank(basePayOrderInfo.getCert())) {
			basePayOrderInfo.setCert(ThreeDesUtil.encrypt2HexStr(key, basePayOrderInfo.getCert()));
		}

		request.setAttribute("payOrderInfo", basePayOrderInfo);
		request.setAttribute("payUrl", oriUrl);

		return "/jsp/mobile/jd/autoSubmitPayOrder";
	
    }
    public static void filterCharProcess(BasePayOrderInfo basePayOrderInfo) {
		basePayOrderInfo.setVersion(doFilterCharProcess(basePayOrderInfo.getVersion()));
		basePayOrderInfo.setMerchant(doFilterCharProcess(basePayOrderInfo.getMerchant()));
		basePayOrderInfo.setDevice(doFilterCharProcess(basePayOrderInfo.getDevice()));
		basePayOrderInfo.setTradeNum(doFilterCharProcess(basePayOrderInfo.getTradeNum()));
		basePayOrderInfo.setTradeName(doFilterCharProcess(basePayOrderInfo.getTradeName()));
		basePayOrderInfo.setTradeDesc(doFilterCharProcess(basePayOrderInfo.getTradeDesc()));
		basePayOrderInfo.setTradeTime(doFilterCharProcess(basePayOrderInfo.getTradeTime()));
		basePayOrderInfo.setAmount(doFilterCharProcess(basePayOrderInfo.getAmount()));
		basePayOrderInfo.setCurrency(doFilterCharProcess(basePayOrderInfo.getCurrency()));
		basePayOrderInfo.setNote(doFilterCharProcess(basePayOrderInfo.getNote()));
		basePayOrderInfo.setCallbackUrl(doFilterCharProcess(basePayOrderInfo.getCallbackUrl()));
		basePayOrderInfo.setNotifyUrl(doFilterCharProcess(basePayOrderInfo.getNotifyUrl()));
		basePayOrderInfo.setIp(doFilterCharProcess(basePayOrderInfo.getIp()));
		basePayOrderInfo.setUserType(doFilterCharProcess(basePayOrderInfo.getUserType()));
		basePayOrderInfo.setUserId(doFilterCharProcess(basePayOrderInfo.getUserId()));
		basePayOrderInfo.setExpireTime(doFilterCharProcess(basePayOrderInfo.getExpireTime()));
		basePayOrderInfo.setOrderType(doFilterCharProcess(basePayOrderInfo.getOrderType()));
		basePayOrderInfo.setIndustryCategoryCode(doFilterCharProcess(basePayOrderInfo.getIndustryCategoryCode()));
		basePayOrderInfo.setSpecCardNo(doFilterCharProcess(basePayOrderInfo.getSpecCardNo()));
		basePayOrderInfo.setSpecId(doFilterCharProcess(basePayOrderInfo.getSpecId()));
		basePayOrderInfo.setSpecName(doFilterCharProcess(basePayOrderInfo.getSpecName()));

		basePayOrderInfo.setVendorId(doFilterCharProcess(basePayOrderInfo.getVendorId()));
		basePayOrderInfo.setGoodsInfo(doFilterCharProcess(basePayOrderInfo.getGoodsInfo()));
		basePayOrderInfo.setOrderGoodsNum(doFilterCharProcess(basePayOrderInfo.getOrderGoodsNum()));
		basePayOrderInfo.setTermInfo(doFilterCharProcess(basePayOrderInfo.getTermInfo()));
		basePayOrderInfo.setReceiverInfo(doFilterCharProcess(basePayOrderInfo.getReceiverInfo()));

	}
    /**
	 * 
	 * @Title: doFilterCharProcess
	 * @Description: 执行特殊字符处理
	 * @param: @param param
	 * @param: @return
	 * @return: String
	 * @throws
	 * @author mythling
	 * @Date 2016年8月6日 下午3:54:58
	 */
	private static String doFilterCharProcess(String param) {
		if (param == null || param.equals("")) {
			return param;
		} else {
			return StringEscape.htmlSecurityEscape(param);
		}
	}
}
